package com.ecgsystem.listadapter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.ecgsystem.constants.Constants;
import com.ecgsystem.R;

public class PatientListAdapter extends BaseAdapter implements Filterable {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private ArrayList<HashMap<String, String>> filteredData;
	private static LayoutInflater inflater = null;
	private int positions[];
	private boolean isOnline[];

	public PatientListAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
		activity = a;
		data = d;
		filteredData = d;
		isOnline = new boolean[data.size()];
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		positions = new int[data.size()];
		for (int i = 0; i < data.size(); i++) {
			positions[i] = i;
		}
	}

	public int getCount() {
		return filteredData.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public static class ViewHolder {
		public TextView patientID;
		public TextView patientName;
		public TextView patientAge;
		public ImageView onlineImage;
	}

	public int[] getPositionList() {
		return positions;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		ViewHolder holder;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.list_item, null);
			holder = new ViewHolder();
			holder.patientID = (TextView) vi
					.findViewById(R.id.textView_patientID);
			holder.patientName = (TextView) vi
					.findViewById(R.id.textView_patientName);
			holder.patientAge = (TextView) vi
					.findViewById(R.id.textView_patientAge);
			holder.onlineImage = (ImageView) vi
					.findViewById(R.id.imageView_patient);
			vi.setTag(holder);
		} else {
			holder = (ViewHolder) vi.getTag();
		}

		holder.patientID.setText(filteredData.get(position).get(
				Constants.TAG_PATIENT_ID));
		holder.patientName.setText(filteredData.get(position).get(
				Constants.TAG_PATIENT_NAME));

		String dateSt = filteredData.get(position).get(
				Constants.TAG_PATIENT_AGE);

		String ages = getAge(Integer.valueOf(dateSt.substring(0, 4)),
				Integer.valueOf(dateSt.substring(5, 7)),
				Integer.valueOf(dateSt.substring(8, 10)));
		holder.patientAge.setText(ages);
		
		if(isOnline[position] == true){
			holder.onlineImage.setImageResource(R.drawable.online);
		}else{
			holder.onlineImage.setImageResource(R.drawable.offline);
		}
		return vi;
	}

	public static String getAge(int year, int month, int day) {
		Calendar dob = Calendar.getInstance();
		Calendar today = Calendar.getInstance();

		dob.set(year, month, day);
		int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

		if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
			age--;
		}
		Integer ageInt = Integer.valueOf(age);
		String ageS = ageInt.toString();

		return ageS;
	}

	public Filter getFilter() {
		Filter filter = new Filter() {
			@Override
			protected FilterResults performFiltering(CharSequence constraint) {
				FilterResults retval = new FilterResults();
				retval.values = data;
				retval.count = data.size();
				if (constraint != null && constraint.toString().length() > 0) {
					constraint = constraint.toString().toUpperCase();
					ArrayList<HashMap<String, String>> filteredData = new ArrayList<HashMap<String, String>>();
					HashMap<String, String> item;

					int j = 0;
					for (int i = 0; i < data.size(); i++) {
						item = data.get(i);
						if (item.get(Constants.TAG_PATIENT_ID).toUpperCase()
								.contains(constraint)
								|| item.get(Constants.TAG_PATIENT_NAME)
										.toUpperCase().contains(constraint)) {
							filteredData.add(item);

							// update position array
							positions[j] = i;
							j++;
						}
					}
					retval.count = filteredData.size();
					retval.values = filteredData;
				} else {
					synchronized (this) {
						retval.count = data.size();
						retval.values = data;
					}
				}
				return retval;
			}

			@Override
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				filteredData = (ArrayList<HashMap<String, String>>) results.values;

				notifyDataSetChanged();

			}
		};
		return filter;
	}
	
	public void setOnlineList(int count, String[] onlineList){
		for(int i = 0; i < filteredData.size(); i++){
			for( int j = 0; j < count; j++){
				if(filteredData.get(i).get(Constants.TAG_PATIENT_ID).equals(onlineList[j])){
					isOnline[i] = true;
					break;
				}else {
					isOnline[i] = false;
				}
			}
		}
		notifyDataSetChanged();
	}
}